﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Radiation___IM.Commands
{
    public class SetPassword : ICommand
    {
        private readonly string m_commandkey = "set_password";

        public SetPassword()
        {
        }

        public string commandKey()
        {
            return m_commandkey;
        }

        public string requiredPermission()
        {
            return null;
        }

        public void execute(string[] command_parts, Client client)
        {
            if (!client.Nickname.StartsWith("guest"))
            {
                if (Users.UserManager.Instance[client.Nickname] != null)
                {
                    if (command_parts.Length > 1 && command_parts[1].Length > 0)
                    {
                        Users.User user = Users.UserManager.Instance[client.Nickname];
                        user.Password = command_parts[1];
                        Data.DatabaseWriter.Instance.Queue(user.getFile());
                        MessageExecution.sendData(client, "Your new password is set.");
                    }
                    else
                        MessageExecution.sendData(client, "Please type a password (can't be empty)");
                }
                else
                    MessageExecution.sendData(client, "Your nickname doen't have it's own account. Create one before setting your password");                    
            }
            else
                MessageExecution.sendData(client, "Your username ( "+client.Nickname+" ) is not valid because it's a default one.");
        }
    }
}
